var columns= [ { title: '序号',width:'5%',formatter: function (value, row, index) {return index+1;}},
               { title: '名称',field: 'name'},
               //{ title: '编码', field: 'code' },
               { title: '排序', field: 'sorted' },
               { title: '操作' ,formatter: function (value, row, index) {
            	   var str=JSON.stringify(row);
            	   str=str.replace(/\"/g,"'");
				   return '<a href="javascript:modify('+str+')">修改</a>'+
				   			'<a href="javascript:deleteData('+row.id+')" style="margin-left:10px;">删除</a>';
            	   }}
           ];
var multiColumns= [ { title: '序号',width:'5%',formatter: function (value, row, index) {return index+1;}},
               { title: '名称',field: 'name'},
               //{ title: '编码', field: 'code' },
               { title: '描述', field: 'description' },
               { title: '操作' ,formatter: function (value, row, index) {
            	   var str=JSON.stringify(row);
            	   str=str.replace(/\"/g,"'");
				   return '<a href="javascript:updateTreeNode('+row.id+')">修改</a>'+
				   '<a href="javascript:deleteTreeNode('+row.id+')" style="margin-left:10px;">删除</a>';
            	   }}
           ];
var yearColumns=[ { title: '序号',width:'5%',formatter: function (value, row, index) {return index+1;}},
                  { title: '国家',field: 'country'},
                  { title: '朝代', field: 'dynasty' },
                  { title: '年号', field: 'years' },
                  { title: '起始年',field:'startyear'},
                  { title: '结束年',field:'endyear'},
                  { title: '君主',field:'emperor'},
                  { title: '备注',field:'remark'},
                  { title: '操作' ,formatter: function (value, row, index) {
               	   var str=JSON.stringify(row);
               	   str=str.replace(/\"/g,"'");
					  return '<a href="javascript:modifyYearTable('+str+')">修改</a>'+
					  '<a href="javascript:deleteYear('+row.id+')" style="margin-left:10px;">删除</a>';
               	   }}
              ];
var addFlag;
var addMultiFlag;
var zTreeObj;
var treeNodes;
var SelectedId="";
var SelectedName="";
var tblname="";
var tblnameText="";
var tblType='1';
var settingZtree = { 
    data: {  
        simpleData: {  
            enable: true,  
            idKey: "id",  
            pIdKey: "pId",  
            rootPId: 0  
        }
    },
    callback: {
    	onClick: zTreeOnClick    	
	}
};
	$(document).ready(function(){
		tblname='mb_gy_byfl';
		tblnameText="八音分类基表";
		createTable('#data-table', 'MbXtglBaseTableServlet.getData.do', 'id', columns,{tn:tblname});
		$(".btButton").click(function(){
			   var list = $(this).attr('value').split("&");
			  tblname=list[0];
			  tblType=$(this).attr('tableType');
			  tblnameText=list[1];
			  var tableTxt=$(this).html();
			  $("#tableName").html(tableTxt);
			  changeTabel2(tblname,tblType);
			  $('#multi-table').bootstrapTable('destroy');
			  
		});
	});
	function changeTabel1(){
		var value=$('#table1').val();
		var html='';
		if(value==0){
			html+='<option value="mb_gy_byfl">八音分类</option>';
			html+='<option value="mb_gy_yqzl">乐器种类</option>';
			html+='<option value="mb_gy_zq">族群</option>';
			html+='<option value="mb_gy_psjd">拍摄角度</option>';
			html+='<option value="mb_gy_jljzlx">记录介质类型表</option>';
			html+='<option value="mb_gy_wjgs">文件格式表</option>';
			html+='<option value="mb_gy_sxcjgs">声像采集格式</option>';
			html+='<option value="mb_gy_lyfs">采集方式</option>';
			html+='<option value="mb_gy_mb_gy_cyyz">常用语种基表</option>';
			html+='<option value="mb_gy_fsjz">发声机制</option>';
			html+='<option value="mb_gy_bblx">标本类型</option>';
			html+='<option value="mb_gy_syysjq">声源原始激起</option>';
			html+='<option value="mb_gy_zyqxlx">资源权限类型</option>';
			
		}else if(value==1){
			html+='<option value="mb_gy_nd">年代</option>';
		}else if(value==2){
			html+='<option value="mb_gy_hsfl">霍萨分类</option>';
			html+='<option value="mb_gy_mjfl">民间分类</option>';
			html+='<option value="mb_gy_yqfl">乐器分类</option>';
			html+='<option value="mb_gy_yy">语源</option>';
			html+='<option value="mb_gy_cz">材质</option>';
			html+='<option value="mb_gy_dq">地区</option>';			
		}
		$('#table2').html(html);
		changeTabel2();
	}
	
	function changeTabel2(value,tableType){
		if(tableType=='1'){
			$("#normaldiv").show();
			$("#multidiv").hide();
			$('#data-table').bootstrapTable('destroy');
			createTable('#data-table', 'MbXtglBaseTableServlet.getData.do', 'id', columns,{tn:value});
		}else if(tableType=='2'){
			$("#normaldiv").show();
			$("#multidiv").hide();
			$('#data-table').bootstrapTable('destroy');
			createTable('#data-table', 'MbXtglBaseTableServlet.getData.do', 'id', yearColumns,{tn:value});
		}else if(tableType=='3'){
			$("#normaldiv").hide();
			$("#multidiv").show();
			treeNodes = ajaxResult("post",false,"MbXtglBaseTableServlet.getTreeData.do",{'tn':value});
			if(treeNodes.data!=null){
				zTreeObj = $.fn.zTree.init($("#ztree1"), settingZtree, treeNodes.data);
				//设置默认选择第一个节点
				SelectedId = treeNodes.data[0].id;
				SelectedName = treeNodes.data[0].name;
				var NodeObject = zTreeObj.getNodeByParam("id",SelectedId,null);
				zTreeObj.selectNode(NodeObject);
			}			
		}		
	}
	function addData(){
		 
		addFlag='add';
		$("#myModalLabel").html("添加基表数据");
		if(tblType=='1'){
			$("#addForm")[0].reset();
			$('#basictableModal').modal('show');
		}else if(tblType=='2'){
			$("#yearsForm")[0].reset();
			$('#yeartableModal').modal('show');
		}
		
	}
	//普通基表modal确认添加触发
	function addConfirm(){
		if(!checkform("#addForm")){
			return;
		}
		var params =$('#addForm').serializeObject();
		params.tn=tblname; 
		var url;
		var words;
		if(addFlag=='add'){
			url='MbXtglBaseTableServlet.insert.do';
			words='添加成功';
		}else{
			url='MbXtglBaseTableServlet.update.do';
			words='修改成功';
		}
		var result=ajaxResult('GET',false,url,params);
		if(result.status=='OK'){
			alertFunc(words,function(){
				$('#data-table').bootstrapTable('refresh');
				$('#basictableModal').modal('hide');
				$('#name').val('');
				$('#code').val('');
				$('#description').val('');
			 });
		}else{
			alertFunc(result.msg);
		}
	}
	//年代基表确认添加按钮触发
	function addYearTableConfirm(){
		if(!checkform("#yearsForm")){
			return;
		}
		var params =$('#yearsForm').serializeObject();
		params.tn=tblname;
		var url;
		var words;
		if(addFlag=='add'){
			url='MbXtglBaseTableServlet.insertNd.do';
			words='添加成功';
		}else{
			url='MbXtglBaseTableServlet.updateNd.do';
			words='修改成功';
		}
		var result=ajaxResult('GET',false,url,params);
		if(result.status=='OK'|result.status=='ok'){
			alertFunc(words,function(){
				$('#data-table').bootstrapTable('refresh');
				$('#yeartableModal').modal('hide');
			 });
		}else{
			alertFunc(result.msg);
		}
	}
	function deleteData(id){
		var result=ajaxResult('GET',false,'MbXtglBaseTableServlet.delete.do',{tn:tblname,id:id}); 
		if(result.status=='OK'){
			alertFunc('删除成功',function(){
				$('#data-table').bootstrapTable('refresh');
			 });
		}else{
			alertFunc(result.msg);
		}
	}
	function deleteYear(id){
		var result=ajaxResult('GET',false,'MbXtglBaseTableServlet.deleteNd.do',{tn:tblname,id:id}); 
		if(result.status=='OK'){
			alertFunc('删除成功',function(){
				$('#data-table').bootstrapTable('refresh');
			 });
		}else{
			alertFunc(result.msg);
		}
	}
	function modify(data){
		addFlag='modify';
		$('#sortedRow').css('display','block');
		$('#sorted').attr('name','sorted');
		$("#myModalLabel").html("修改基表数据");
		$('#basictableModal').modal('show');
		$('#name').val(data.name);
		$('#code').val(data.code);
		$('#description').val(data.description);
		$('#sorted').val(data.sorted);
		$('#id').val(data.id);
	}
	function modifyYearTable(data){
		addFlag='modify';
		$('#country').val(data.country);
		$('#Dynasty').val(data.Dynasty);
		$('#years').val(data.years);
		$('#startYear').val(data.startYear);
		$('#endYear').val(data.endYear);
		$('#emperor').val(data.emperor);
		$('#remark').val(data.remark);
		$('#yeartableModal').modal('show');
		$('#yearid').val(data.id);
	}
	
	//ztree节点点击事件
	function zTreeOnClick(event, treeId, treeNode){
		//生成对应的列表
		SelectedId = treeNode.id;
		SelectedName = treeNode.name;
		$('#parentid').val(treeNode.id);				
		$('#parentname').val(treeNode.name);
		$('#multi-table').bootstrapTable('destroy');
		createTable('#multi-table', 'MbXtglBaseTableServlet.getSonData.do', 'id', multiColumns,{'tn':tblname,'parentid':SelectedId});
	}
	
	//删除树节点
	function deleteTreeNode(id){
		var delid;
		(id!=null)?delid=id:delid=SelectedId;
		if(delid==0){
			alertFunc("请选择要删除的节点！");
			return false;
		}	
		bootbox.confirm("该节点下的所有子节点都将删除，删除后不能恢复，确认删除当前记录吗？",function(r){
		if(r){			
			   var result="";
			    result = ajaxResult("post",false,"MbXtglBaseTableServlet.deleteTwoTable.do",{'tn':tblname,'id':delid});
			    SelectedId="";
			    SelectedName="";			   
			   $('#parentid').val("");
			   $('#parentname').val("");
			if(result["status"] == 'OK'){
				refreshTreeNode();
				$('#multi-table').bootstrapTable('refresh');
			}
		  }
		});
	}
	
	//刷新树
	function refreshTreeNode(){	
		treeNodes = ajaxResult("post",false,"MbXtglBaseTableServlet.getTreeData.do",{'tn':tblname});
		zTreeObj = $.fn.zTree.init($("#ztree1"), settingZtree, treeNodes.data);
	   if(SelectedId==""){
		 	//设置默认选择第一个节点
		    SelectedId = treeNodes.data[0].id;
			SelectedName = treeNodes.data[0].name;
	   }		
	   var NodeObject = zTreeObj.getNodeByParam("id",SelectedId,null);
	   zTreeObj.selectNode(NodeObject);		
	}
	
	//打开树节点的增加窗口
	function openTreeModalAdd(){ 
		if(SelectedId==0){
			alertFunc("请选择父节点！");
			return false;
		}	
		addMultiFlag="add";
		$('#form_multi')[0].reset(); 
		jbtext=tblname;
		//$("#multiModalLabel").text('新增'+jbtext+'数据');
		$("#parentname").val(SelectedName);	
		$("#parentid").val(SelectedId);	
		$("#btn_add").text("增加");
		$('#multiModal').modal('show');
	}
	
	//打开树节点的增加窗口（根节点）
	function openTopTreeModalAdd(){		
		addMultiFlag="add";
		$('#form_multi')[0].reset(); 
		jbtext=tblname;
		//$("#multiModalLabel").text('新增'+jbtext+'数据');
		$("#parentname").val("无");	
		$("#parentid").val(0);	
		$("#btn_add").text("增加");
		$('#multiModal').modal('show');
	}

	//打开树节点的修改窗口
	function updateTreeNode(id){	
		var modid;
		(id!=null)?modid=id:modid=SelectedId;
		if(modid==0){
			alertFunc("请选择要修改的节点！");
			return false;
		}			
		addMultiFlag="modify";
		jbtext=tblnameText;
		$("#multiModalLabel").text('修改'+jbtext+'数据');
		$('#form_multi')[0].reset(); 
		var result=ajaxResult('GET',false,'MbXtglBaseTableServlet.getOneDetail.do',{'tn':tblname,'id':modid});
		if(result.status=='OK'){
			 loadData("#form_multi",result.rows);
		}else{
			alertFunc(result.msg);
		}
		if($("#parentname").val()==""){
			$("#parentname").val("无");
		}					
		$("#btn_add").text("修改");
		$('#multiModal').modal('show');
	}
	
	function addMultiConfirm(){
		var params =$('#form_multi').serializeObject();
		params.tn=tblname; 
		var url;
		var words;
		if(addMultiFlag=='add'){
			url='MbXtglBaseTableServlet.insert.do';
			words='增加成功';
		}else{
			url='MbXtglBaseTableServlet.update.do';
			words='修改成功';
		}
		var result=ajaxResult('GET',false,url,params);
		if(result.status=='OK'){
			alertFunc(words,function(){
				refreshTreeNode();
				$('#multi-table').bootstrapTable('refresh');
				$('#multiModal').modal('hide');				
			 });
		}else{
			alertFunc(result.msg);
		}
	}

	